package com.ruoyi.central.controller.custom;

import com.ruoyi.central.domain.TopCentralMaterial;
import com.ruoyi.central.domain.TopCentralProductTypeOperationStation;
import com.ruoyi.central.enums.AreaTypeEnum;
import com.ruoyi.central.enums.MaterialStatusEnum;
import com.ruoyi.central.service.ITopCentralMaterialService;
import com.ruoyi.central.service.ITopCentralProductTypeOperationStationService;
import com.ruoyi.central.utils.pojo.OperationStationUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.EnumUtils;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Classname StationSelectionController
 * @Author: sunluquan
 * @Date: 2022/5/8 16:07
 * @Description TODO
 */
@RestController
@RequestMapping("central/stationSelection")
public class StationSelectionController {

    @Autowired
    private ITopCentralMaterialService materialService;

//    @Autowired
//    private ITopCentralOperationStationMaterialService operationStationMaterialService;

    @Autowired
    private ITopCentralProductTypeOperationStationService productTypeOperationStationService;
    /**
     * 获取区域列表
     * 将未分组的和分组的操作站位添加导区域列表中
     * @return
     */
    @GetMapping("/list")
    public AjaxResult get(){

        return AjaxResult.success(OperationStationUtils.getOperationStationList());

    }

    /**
     * 1.获取当前站位可加工的物料类型
     * 2.
     * @param operationStationId
     * @return
     */
    @GetMapping("/getFreeMaterialListByOperationStationId/{operationStationId}")
    public AjaxResult getFreeMaterialListByOperationStationId(@PathVariable("operationStationId") Integer operationStationId){

        //获取物料类型 于 站位之间的关系
        List<TopCentralProductTypeOperationStation> productTypeOperationStations = CollectionUtils.filter(productTypeOperationStationService.selectAllMap().values(), (item) -> StringUtils.equals(item.getOperationStationId(), operationStationId));
        //物料类型编号集合
        List<Integer> materialTypeIds = CollectionUtils.mapToList(productTypeOperationStations, TopCentralProductTypeOperationStation::getProductTypeId);
        //下面这段代码不需要的原因 如果物料已经被使用了 会标记物料的状态为使用中或者已完成 所以无需在查询当前物料是否绑定了工位
//        //获取 物料于站位绑定关系 列表
//        List<TopCentralOperationStationMaterial> operationStationMaterials = operationStationMaterialService.selectListByOperationStationId(operationStationId);
//
//        // 绑定站位下的 物料编号集合
//        Set<String> materialIds = CollectionUtils.mapToSet(operationStationMaterials, TopCentralOperationStationMaterial::getMaterialId);

        //根据站位绑定的物料类型集合  获取所有物料
        List<TopCentralMaterial> materials = materialService.selectByMaterialTypeIds(materialTypeIds);

        //判断一个物料是否可以用在当前工位的条件
        //1 物料录入时的操作工位必须为当前工位
        //2 物料状态必须为空闲状态
        List<TopCentralMaterial> freeMaterials = CollectionUtils.filter(materials, (material) -> StringUtils.equals(operationStationId,material.getOperationStationId()) &&
                EnumUtils.equals(MaterialStatusEnum.FREE_OF_STATUS, material.getMaterialStatus()));

        return AjaxResult.success(SelectUtils.buildSelectList(freeMaterials,TopCentralMaterial::getId,true));
    }

}
